## Figure A5 ###
## This is only the code to make the graph####
## The code to create the underlying data is in Figure_A5.do in CODE/GRAPHS/Figure_A5.do

## Load reuqired packages
library(haven)
library(ggplot2)
library(dplyr)
library(arm)
library(modelsummary)
library(car)
library(grid)
library(gridExtra)
library(ggsignif)

## Set working directory to JOP Replication files on your computer 

contact_plot<- read_dta("DATA FILES TO SHARE/TEMP_FILES/Fig_A5.dta")

# WE ARE NOT GOING TO PLOT THE "NONE" FAMILY SATUS SO WE CAN DROP IT 

contact_plot <- contact_plot[contact_plot$order <4,]

# Convert the following into factors 
contact_plot$Family <- as.factor(contact_plot$Family)
contact_plot$group <- as.factor(contact_plot$group)

# assign the factor labels 
contact_plot$group <- factor(contact_plot$group, 
                         labels = c("govtvisit", "civic_index", "claim_procedure", "claim_prob"), 
                         levels = c(1, 2, 3, 4))

contact_plot$Family <- factor(contact_plot$Family, 
                             labels = c("All", "Only husband", "Only-in laws"), 
                             levels = c(1, 2, 3))

## Make the plot 

p1<-ggplot(data=contact_plot[contact_plot$group =="claim_prob",], aes(x= reorder(Family,order) , y=mean)) +
  geom_point(stat="identity", color="black", position=position_dodge())+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.1,
                position=position_dodge(.9), color = "blue")+
  
  
  theme_minimal()+
  #scale_fill_manual(values = c("Yes" = yes_color,"No" = no_color))+
  
  #guides(color = guide_legend(title = "Yes/No")) +
  ylim(0.4,0.6)+
  theme(text = element_text(family = "Arial", size = 13),
        plot.title = element_text(hjust = 0.5, family = "Arial", size = 9),
        plot.caption = element_text(hjust = 0, family = "Courier", size = 9),
        #legend.position = c(.8,.9),
        axis.text = element_text(family = "Arial", size = 12),
        legend.text = element_text(family = "Arial", size = 12),
        legend.title = element_text(family = "Arial", size = 10))+
  labs(
    #title = "What are the big issues in your village? And, did you contact anyone about it",
    subtitle = "Contact with State",
    #caption = "Reference category for in-laws is in-laws dead",
    #tag = "Figure 1",
    x = "", 
    y = "Likelihood",
    #colour = "Husband Prompt"
  )#+ guides(color = guide_legend(title = "Yes/No")) 

#### claim_procedure
p2<-ggplot(data=contact_plot[contact_plot$group =="claim_procedure",], aes(x= reorder(Family,order) , y=mean)) +
  geom_point(stat="identity", color="black", position=position_dodge())+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.1,
                position=position_dodge(.9), color = "blue")+
  
  
  theme_minimal()+
  #scale_fill_manual(values = c("Yes" = yes_color,"No" = no_color))+
  
  #guides(color = guide_legend(title = "Yes/No")) +
  #ylim(0.4,0.6)+
  theme(text = element_text(family = "Arial", size = 13),
        plot.title = element_text(hjust = 0.5, family = "Arial", size = 12),
        plot.caption = element_text(hjust = 0, family = "Courier", size = 12),
        #legend.position = c(.8,.9),
        axis.text = element_text(family = "Arial", size = 12),
        legend.text = element_text(family = "Arial", size = 12),
        legend.title = element_text(family = "Arial", size = 10))+
  labs(
    #title = "What are the big issues in your village? And, did you contact anyone about it",
    subtitle = "Prepare documents",
    #caption = "Reference category for in-laws is in-laws dead",
    #tag = "Figure 1",
    x = "", 
    y = "Likelihood",
    #colour = "Husband Prompt"
  )#+ guides(color = guide_legend(title = "Yes/No")) 

#### CIVIC INDEX
p3<-ggplot(data=contact_plot[contact_plot$group =="civic_index",], aes(x= reorder(Family,order) , y=mean)) +
  geom_point(stat="identity", color="black", position=position_dodge())+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.1,
                position=position_dodge(.9), color = "blue")+
  
  
  theme_minimal()+
  #scale_fill_manual(values = c("Yes" = yes_color,"No" = no_color))+
  
  #guides(color = guide_legend(title = "Yes/No")) +
  #ylim(0.4,0.6)+
  theme(text = element_text(family = "Arial", size = 13),
        plot.title = element_text(hjust = 0.5, family = "Arial", size = 12),
        plot.caption = element_text(hjust = 0, family = "Courier", size = 12),
        #legend.position = c(.8,.9),
        axis.text = element_text(family = "Arial", size = 12),
        legend.text = element_text(family = "Arial", size = 12),
        legend.title = element_text(family = "Arial", size = 10))+
  labs(
    #title = "What are the big issues in your village? And, did you contact anyone about it",
    subtitle = "Civic Participation",
    #caption = "Reference category for in-laws is in-laws dead",
    #tag = "Figure 1",
    x = "Co-residence with gatekeepers", y = "Likelihood",
    #colour = "Husband Prompt"
  )#+ guides(color = guide_legend(title = "Yes/No")) 

### governmetn institution
p4<-ggplot(data=contact_plot[contact_plot$group =="govtvisit",], aes(x= reorder(Family,order) , y=mean)) +
  geom_point(stat="identity", color="black", position=position_dodge())+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.1,
                position=position_dodge(.9), color = "blue")+
  
  
  theme_minimal()+
  #scale_fill_manual(values = c("Yes" = yes_color,"No" = no_color))+
  
  #guides(color = guide_legend(title = "Yes/No")) +
  #ylim(0.4,0.6)+
  theme(text = element_text(family = "Arial", size = 13),
        plot.title = element_text(hjust = 0.5, family = "Arial", size = 12),
        plot.caption = element_text(hjust = 0, family = "Courier", size = 12),
        #legend.position = c(.8,.9),
        axis.text = element_text(family = "Arial", size = 12),
        legend.text = element_text(family = "Arial", size = 12),
        legend.title = element_text(family = "Arial", size = 10))+
  labs(
    #title = "What are the big issues in your village? And, did you contact anyone about it",
    subtitle = "Visit to government office",
    #caption = "Reference category for in-laws is in-laws dead",
    #tag = "Figure 1",
    x = "Co-residence with gatekeepers", y = "Likelihood",
    #colour = "Husband Prompt"
  )#+ guides(color = guide_legend(title = "Yes/No")) 

jf <-grid.arrange(p1, p2, p3, p4, ncol=2)

ggsave("OUTPUT/GRAPHS/FigureA5.png", plot = jf, width = 10, height = 6, dpi = 150, units = "in")
